<template>
  <!-- 表单 -->
  <el-form ref="addForm" :model="formData" label-width="120px">
    <el-form-item label="姓名" prop="username">
      <el-input
        v-model="formData.username"
        style="width: 50%"
        placeholder="请输入姓名"
      />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input
        v-model="formData.mobile"
        style="width: 50%"
        placeholder="请输入手机号"
      />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker
        v-model="formData.timeOfEntry"
        style="width: 50%"
        placeholder="请选择入职时间"
      />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select
        v-model="formData.formOfEmployment"
        style="width: 50%"
        placeholder="请选择"
      >
        <el-option
          v-for="item in employee"
          :key="item.id"
          :value="item.id"
          :label="item.label"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input
        v-model="formData.workNumber"
        style="width: 50%"
        placeholder="请输入工号"
      />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input
        v-model="formData.departmentName"
        style="width: 50%"
        placeholder="请选择部门"
        @focus="hFocus"
      />
      <div v-if="showTree">
        <el-tree
          :data="treelist"
          :props="{ label: 'name' }"
          @node-click="hNodeClick"
        />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker
        v-model="formData.correctionTime"
        style="width: 50%"
        placeholder="请选择转正时间"
      />
    </el-form-item>
    <el-form-item>
      <el-button @click="hCancel">取消</el-button>
      <el-button type="primary" @click="hSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { arraytoTree } from '@/utils/index'
import { getDepartments } from '@/api/departments'
import { addEmployee } from '@/api/employees'
const TYPE_CODE = { 1: '正式', 2: '非正式' }
export default {
  name: 'EmployAdd',
  data() {
    return {
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      employee: Object.keys(TYPE_CODE).map((key) => {
        return {
          id: key,
          label: TYPE_CODE[key]
        }
      }),
      treelist: [],
      showTree: false
    }
  },
  created() {},
  methods: {
    async doAdd() {
      try {
        await addEmployee(this.formData)
        this.$emit('success')
        this.$refs.addForm.resetFields()
      } catch (error) {
        console.log(error)
      }
    },
    hNodeClick(data, node) {
      if (data.children.length) {
        return
      }
      this.formData.departmentName = data.name
      this.showTree = false
    },
    hFocus() {
      this.showTree = true
      this.loadEmpList()
    },
    async loadEmpList() {
      const res = await getDepartments()
      console.log(res)
      res.data.depts.shift() // 去掉第一个元素（公司名）
      this.treelist = arraytoTree(res.data.depts)
    },
    hCancel() {
      this.$emit('hclose')
    },
    hSubmit() {
      this.doAdd()
    }
  }
}
</script>
